package com.zkjl.protect_business.service;

import cn.hutool.core.collection.CollUtil;
import com.zkjl.protect_business.entity.SysRole;
import com.zkjl.protect_business.entity.User;
import com.zkjl.protect_business.entity.UserPrincipal;
import com.zkjl.protect_business.repository.SysRoleRepository;
import com.zkjl.protect_business.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 自定义UserDetails查询
 * </p>
 */
@Service
public class CustomUserDetailsService implements UserDetailsService {
    @Autowired
    UserRepository userRepository;
    @Autowired
    SysRoleRepository roleRepository;


    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {

        User user = userRepository.findByUserName(userName);
        if (user == null) {
            throw new UsernameNotFoundException("未找到用户信息 : " + userName);
        }
        return UserPrincipal.create(user);
    }
}
